import cv2


def Mean_Filtering(img, ksize):  # 均值滤波
    image = cv2.imread(img)
    result = cv2.blur(image, (ksize, ksize))
    result_rgb = cv2.cvtColor(result, cv2.COLOR_BGR2RGB)
    return result_rgb


def Gaussian_Filtering(img, ksize, sigmaX):  # 高斯滤波
    image = cv2.imread(img)
    result = cv2.GaussianBlur(image, (ksize, ksize), sigmaX)
    result_rgb = cv2.cvtColor(result, cv2.COLOR_BGR2RGB)
    return result_rgb


def Median_Filtering(img, ksize):  # 中值滤波
    image = cv2.imread(img)
    result = cv2.medianBlur(image, ksize)
    result_rgb = cv2.cvtColor(result, cv2.COLOR_BGR2RGB)
    return result_rgb


def Bilateral_Filtering(img, d, sigmaColor, sigmaSpace):  # 双边滤波
    image = cv2.imread(img)
    result = cv2.bilateralFilter(image, d, sigmaColor, sigmaSpace)
    result_rgb = cv2.cvtColor(result, cv2.COLOR_BGR2RGB)
    return result_rgb


def Laplacian_Operator(img, ksize, scale, delta):  # 拉普拉斯算子锐化
    image = cv2.imread(img)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    if ksize > 31:
        ksize = 31
    laplacian = cv2.Laplacian(
        gray, cv2.CV_64F, ksize=ksize, scale=scale, delta=delta)
    result = cv2.convertScaleAbs(laplacian)
    return result


# 非锐化掩膜
def Unsharp_Masking(img, ksize, sigmaX, img_weight, blurred_weight):
    image = cv2.imread(img)
    blurred = cv2.GaussianBlur(image, (ksize, ksize), sigmaX)

    # 创建掩模
    mask = cv2.addWeighted(image, img_weight, blurred, blurred_weight, 0)
    mask_rgb = cv2.cvtColor(mask, cv2.COLOR_BGR2RGB)
    return mask_rgb
